package com.yy.young.travel.web;

import com.yy.young.common.core.excel.ExcelExport;
import com.yy.young.common.core.excel.ExcelImport;
import com.yy.young.common.core.excel.IExcelExport;
import com.yy.young.common.core.excel.IExcelImport;
import com.yy.young.common.util.CommonUtil;
import com.yy.young.common.util.Result;
import com.yy.young.common.util.StringUtils;
import com.yy.young.dal.util.Page;
import com.yy.young.interfaces.log.annotation.Log;
import com.yy.young.interfaces.model.User;
import com.yy.young.travel.model.NatureReserveCheck;
import com.yy.young.travel.model.Reserve;
import com.yy.young.travel.model.ReserveCategory;
import com.yy.young.travel.service.INatureReserveCheckService;
import com.yy.young.travel.service.IReserveCategoryService;
import com.yy.young.travel.service.IReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* 自然保护地详情表实体类服务
* Created by rookie on 2018-04-03.
*/
@Controller
@RequestMapping("/travel/reserve")
public class ReserveController {

    @Resource(name="reserveService")
    IReserveService service;

    @Autowired
    INatureReserveCheckService iNatureReserveCheckService;
    @Autowired
    IReserveCategoryService iReserveCategoryService;
    /**
     * 所属部门统计分析
     */
    @Log("所属部门统计分析")
    @RequestMapping("/deptStatis")
    @ResponseBody
    public Object DeptStatis(HttpServletRequest request) throws Exception{
    	return new Result(service.deptStatis());
    }
    /**
     * 自然保护地面积统计  按类型分组
     */
    @Log("自然保护地面积统计分析")
    @RequestMapping("/naStatis")
    @ResponseBody
    public Object naStatis(HttpServletRequest request) throws Exception{
    	return new Result(service.naStatis());
    }
    /**
     * 自然保护地类型统计分析
     */
    @Log("自然保护地类型统计分析")
    @RequestMapping("/ncStatis")
    @ResponseBody
    public Object ncStatis(HttpServletRequest request) throws Exception{
    	return new Result(service.ncStatis());
    }
    /**
    * 获取数据列表
    * @param obj
    * @param request
    * @return
    * @throws Exception
    */
    @Log("查询自然保护地详情表实体类列表")
    @RequestMapping("/getList")
    @ResponseBody
    public Object getPmsDeptList(Reserve obj, HttpServletRequest request) throws Exception{
        List<Reserve> list = service.getList(obj);
        Result result = new Result(list);
        return result;
    }

    /**
    * 获取分页数据
    * @param obj
    * @param request
    * @return
    * @throws Exception
    */
    @Log("分页查询自然保护地详情表实体类列表")
    @RequestMapping("/getPage")
    @ResponseBody
    public Object getPmsDeptPage(Reserve obj, HttpServletRequest request) throws Exception{
        Page page = CommonUtil.getPageFromRequest(request);
        List<Reserve> list = service.getPage(obj, page);
        page.setData(list);
        return page;
    }

    /**
    * 获取单条数据
    * @param id
    * @param request
    * @return
    * @throws Exception
    */
    @Log("查询自然保护地详情表实体类")
    @RequestMapping("/get")
    @ResponseBody
    public Object getPmsDept(String id, HttpServletRequest request) throws Exception{
        Reserve obj = service.get(id);
        return new Result(obj);
    }
    /**
     * 获取单条数据  把保护地类型 id转为name
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询自然保护地详情表实体类")
    @RequestMapping("/getReserve")
    @ResponseBody
    public Object getReserve(String id, HttpServletRequest request) throws Exception{
        Reserve obj = service.get(id);
        ReserveCategory reserveCategory = iReserveCategoryService.get(obj.getCategoryId());
        obj.setCategoryId(reserveCategory.getName());
        return new Result(obj);
    }

    /**
    * 新增
    * @param obj
    * @param request
    * @return
    * @throws Exception
    */
    @Log("新增自然保护地详情表实体类")
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(Reserve obj, HttpServletRequest request) throws Exception{
        service.insert(obj);
        return new Result();
    }

    /**
    * 修改
    * @param obj
    * @param request
    * @return
    * @throws Exception
    */
    @Log("修改自然保护地详情表实体类")
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Reserve obj, HttpServletRequest request) throws Exception{
        service.update(obj);
        return new Result();
    }
    /**
     * 审批不通过后再次上报
     * @param obj
     * @param request
     * @return
     * @throws Exception
     */
    @Log("再次上报")
    @RequestMapping("/saveAgain")
    @ResponseBody
    public Object saveAgain(Reserve obj, HttpServletRequest request) throws Exception{
        obj.setState("1");
        obj.setCreateTime(new Date());
        service.update(obj);
        NatureReserveCheck natureReserveCheck = iNatureReserveCheckService.get(obj.getId());
        natureReserveCheck.setState("1");
        natureReserveCheck.setName(obj.getName());
        ReserveCategory reserveCategory = iReserveCategoryService.get(obj.getCategoryId());
        natureReserveCheck.setCategoryName(reserveCategory.getName());
        iNatureReserveCheckService.update(natureReserveCheck);
        return new Result();
    }

    /**
    * 删除
    * @param ids
    * @param id
    * @param request
    * @return
    * @throws Exception
    */
    @Log("删除自然保护地详情表实体类")
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String ids, String id, HttpServletRequest request) throws Exception{
        Result result = new Result();
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            service.delete(idArr);
        }else if(StringUtils.isNotBlank(id)){
            service.delete(id);
        }else{
            result.setCode(-1);
            result.setInfo("删除失败:待删除编号无效!");
        }
        return result;
    }

    /**
    * 导入
    * @param file
    * @param request
    * @return
    * @throws Exception
    */
    @Log("导入自然保护地详情表实体类")
    @RequestMapping("/import")
    @ResponseBody
    public Object importExcel(MultipartFile file, HttpServletRequest request) throws Exception{
        Result result = new Result();
        try {
            if (file != null && !file.isEmpty()) {
                //excel导入处理,返回excel中的数据集合
                IExcelImport ei = new ExcelImport(file);//将文件转为ExcelImport对象
                //从excel读取数据
                List<Reserve> list = ei.getImportDataAsBean(Reserve.class);
                if (list != null && list.size() > 0){
                    int num = service.batchInsert(list);//批量插入
                    result.setInfo("成功导入数据" + num + "条!");
                }else {
                    result.setCode(-1);
                    result.setInfo("导入失败:excel解析后结果为空!");
                }
            } else {
                result.setCode(-1);
                result.setInfo("导入失败:文件为空!");
            }
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setInfo("导入失败!");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setInfo("导入失败!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setInfo("导入失败!");
        }
        return result;
    }

    /**
    * 导出
    * @param obj 查询的参数
    * @param request
    * @param response
    * @throws Exception
    */
    @Log("导出自然保护地详情表实体类")
    @RequestMapping("/export")
    public void exportExcel(Reserve obj, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Reserve> list = service.getList(obj);
        if (list != null && list.size() > 0){
            IExcelExport ee = new ExcelExport();
            ee.insertBeanList(list, Reserve.class);
            ee.write2Response(response, "excel_" + System.currentTimeMillis() + ".xls");
        }else{
            response.getWriter().write("数据为空!");
        }
    }
    /**
     * 同意调整  获得保护地id  以及审核意见  修改保护地状态为2  修改保护地审核实体类状态为2  设置checkResult
     * @param obj
     * @param request
     * @return
     * @throws Exception
     */
    @Log("修改自然保护地详情表实体类")
    @RequestMapping("/agreeApply")
    @ResponseBody
    public Object agreeApply(NatureReserveCheck obj, HttpServletRequest request) throws Exception{
        Result result = new Result();
        if (StringUtils.isBlank(obj.getId()) || StringUtils.isBlank(obj.getOpinion())){
            result.setCode(-1);
            result.setInfo("操作失败:申请编号或审核意见无效!");
        }else{
                service.agreeApply(obj);//同意
        }
        return result;
    }

    /**
     * 拒绝调整  获得保护地id  以及审核意见  修改保护地状态wei3  修改保护地审核实体类状态为3  设置checkResult
     * @param obj
     * @param request
     * @return
     * @throws Exception
     */
    @Log("修改自然保护地详情表实体类")
    @RequestMapping("/disagreeApply")
    @ResponseBody
    public Object disagreeApply(NatureReserveCheck obj, HttpServletRequest request) throws Exception{
        Result result = new Result();
        if (StringUtils.isBlank(obj.getId()) || StringUtils.isBlank(obj.getOpinion())){
            result.setCode(-1);
            result.setInfo("操作失败:申请编号或审核意见无效!");
        }else{
            service.disagreeApply(obj);//同意
        }
        return result;
    }


}